package com.yq.master.interceptor;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;
import com.alibaba.fastjson.JSONObject;


/**
 * 文件名：session拦截器
 * 创建人：yinquan
 * 创建日期: 2016-12-22 13:43
 * 描述：登录用户session拦截
 * 版本号：1.0
 */

public class SessionInterceptor extends HandlerInterceptorAdapter{

    private static final Logger logger = LoggerFactory.getLogger(SessionInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest request,	HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request,	HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,	ModelAndView modelAndView)throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
    	
        /*if(null==anAppUserBean){
            String requestType = request.getHeader("X-Requested-With");
            String accept = request.getHeader("accept");
            request.setAttribute("errcode", ConstantCode.CODE_ERR_997);
            if("XMLHttpRequest".equals(requestType)){
                if(accept.indexOf("application/json")>-1){//返回json
                    response.setStatus(Integer.valueOf(ConstantCode.CODE_ERR_997));
                    JsonReturn json = new JsonReturn();
                    json.setErrcode(ConstantCode.CODE_ERR_997);
                    json.setErrmsg("当前登录已超时，请重新登录！");
                    PrintWriter pw=response.getWriter();
                    pw.write(JSONObject.toJSONString(json));
                    pw.flush();
                    pw.close();
                    logger.debug("当前登录已超时，请重新登录！");
                }else if(accept.indexOf("text/html")>-1){//返回json
                    response.setStatus(Integer.valueOf(ConstantCode.CODE_ERR_997));
                    JsonReturn json = new JsonReturn();
                    json.setErrcode(ConstantCode.CODE_ERR_997);
                    json.setErrmsg("当前登录已超时，请重新登录！");
                    PrintWriter pw=response.getWriter();
                    pw.write(JSONObject.toJSONString(json));
                    pw.flush();
                    pw.close();
                    logger.debug("当前登录已超时，请重新登录！");
                }else{
                    response.setContentType("text/html; charset=utf-8" );
                    response.sendRedirect(request.getContextPath() + "/error/sessiontimeout.jsp");
                    logger.debug("当前登录已超时，请重新登录！");
                }
            }else{
                response.sendRedirect(request.getContextPath() + "/error/sessiontimeout.jsp");
                logger.debug("正常访问资源请求！");
            }
            return false;
        }else{
            return super.preHandle(request, response, handler);
        }*/
    	
    	return super.preHandle(request, response, handler);
    	
    }
}
